package com.masterclient.app.modules.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;
import com.eoner.baselibrary.bean.goods.CouponMessage;
import com.masterclient.app.modules.order.adapter.CouponChooseAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/8/4 13:17
 * 功能说明：
 */

public class CouponChooseFragment extends BaseFragment {

    public final static int CHOOSEDISCOUNT = 109;
    public static final String UNUSE = "unuse";
    public static final String USELESS = "useless";
    String type;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_bottom)
    TextView tvBottom;
    @BindView(R.id.view_bottom)
    View viewBottom;

    private CouponChooseAdapter adapter;
    private List<CouponMessage> list = new ArrayList<>();
    String ids = "0";

    public static CouponChooseFragment getInstance(String type, List<CouponMessage> list) {
        CouponChooseFragment couponFragment = new CouponChooseFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("type", type);
        bundle.putSerializable("list", (Serializable) list);
        couponFragment.setArguments(bundle);
        return couponFragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_choose_coupon, null);
    }

    @Override
    protected void initOnlyOnce() {
        Bundle bundle = getArguments();
        this.type = bundle.getString("type");
        this.list = (List<CouponMessage>) bundle.getSerializable("list");

        SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
        adapter = new CouponChooseAdapter(getActivity(), layoutHelper, list.size(), null, list, type) {
            @Override
            public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_coupon_goods2, parent, false));
            }

            @Override
            public void onBindViewHolder(MainViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(adapter);

        adapter.setOnCheckChangeListerner((position, check) -> {
            list.get(position).setSelect_status(check);
            if (check) {
                for (int i = 0; i < list.size(); i++) {
                    if (position != i) {
                        list.get(i).setSelect_status(false);
                    }
                }
            }

            //没有计算布局 且没有滚动时
            if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE
                    && (recyclerView.isComputingLayout() == false)) {
                adapter.setData(list);
            }
        });

        if (type.equals("unuse")) {
            viewBottom.setVisibility(View.VISIBLE);
        } else {
            viewBottom.setVisibility(View.GONE);
        }
        if (list == null || list.size() == 0) {
            tvBottom.setBackgroundResource(R.drawable.btn_disable_bkg);
            tvBottom.setTextColor(0x66000000);
            tvBottom.setClickable(false);
        }

        super.initOnlyOnce();
    }

    @OnClick({R.id.tv_bottom})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_bottom:
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getSelect_status()) {
                        ids = list.get(i).getCouponId();
                        break;
                    }
                }
                Intent intent1 = new Intent();
                intent1.putExtra("id", ids);
                getActivity().setResult(CHOOSEDISCOUNT, intent1);
                getActivity().finish();
                break;
        }
    }
}
